package com.hanxiaozhang.fallback;


import com.hanxiaozhang.result.ResultCode;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.util.JsonUtil;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * 功能描述: <br>
 * 〈Zuul回退服务〉
 * todo 20200724 目前所有服务只做一个回退，粒度是否过大？
 *
 * @Author:hanxiaozhang
 * @Date: 2020/7/24
 */
@Slf4j
@Component
public class AllServerFallback implements FallbackProvider {

    /**
     * 获取回退路由规则
     *
     * @return
     */
    @Override
    public String getRoute() {
        // *表示为所有微服务提供回退
        return "*";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        log.info("into AllServerFallback.fallbackResponse:[{}]" + cause.getMessage());
        if (cause instanceof HystrixTimeoutException) {
            return response(HttpStatus.GATEWAY_TIMEOUT);
        } else {
            return response(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 声明ClientHttpResponse
     *
     * @param status
     * @return
     */
    private ClientHttpResponse response(final HttpStatus status) {
        return new ClientHttpResponse() {

            /**
             * fallback时的状态码
             *
             * @return
             * @throws IOException
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return status;
            }

            /**
             * 数字类型的状态码，本例返回的其实就是200，详见HttpStatus
             *
             * @return
             * @throws IOException
             */
            @Override
            public int getRawStatusCode() throws IOException {
                return status.value();
            }

            /**
             * 状态文本，本例返回的其实就是OK，详见HttpStatus
             *
             * @return
             * @throws IOException
             */
            @Override
            public String getStatusText() throws IOException {
                return status.getReasonPhrase();
            }

            @Override
            public void close() {
            }

            /**
             *  响应体
             *
             * @return
             * @throws IOException
             */
            @Override
            public InputStream getBody() throws IOException {
                String content = JsonUtil.beanToJson(Result.error(ResultCode.SERVER_ERROR));
                return new ByteArrayInputStream(content.getBytes());
            }

            /**
             * headers设定
             *
             * @return
             */
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                MediaType mt = new MediaType("application", "json", Charset.forName("UTF-8"));
                headers.setContentType(mt);
                return headers;
            }
        };

    }
}
